package Serialization;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.junit.Test;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * @author wang'ji'cheng
 * @date 2025/7/17
 */
public class Demo {
    public static void main(String[] args) {


    }


    //测试序列化
    @Test
    public void test1() {
        Person person = new Person();
        person.setId(1L);
//        person.setName("张三");
        person.setAge(18);

        person.setBirthday(new Date());
        person.setCreateTime(LocalDateTime.now());
        //SerializerFeature枚举类，可以设置序列化的规则
        // WriteMapNullValue 设置序列化空值
        Object json = JSON.toJSONString(person, SerializerFeature.WriteMapNullValue);
        System.out.println(json);

        ArrayList<Person> list = new ArrayList<>();
        Person person1 = new Person();
        person.setName("李四");
        list.add(person1);
        list.add(person1);
        list.add(person1);
        list.add(person1);
        String jsonString = JSON.toJSONString(list, SerializerFeature.DisableCircularReferenceDetect);
        System.out.println(jsonString);
    }
    @Test
    public void test2(){
        Person person1 = new Person();
        person1.setName("张三");
        person1.setId(1L);
        person1.setBirthday(new Date());

        Person person2 = new Person();
        person2.setName("李四");
        person2.setId(2L);
        person2.setBirthday(new Date());

        HashMap<String, Person> map = new HashMap<String, Person>();

        map.put("张三", person1);
        map.put("李四", person2);

        String jsonString = JSON.toJSONString(map);
        System.out.println(jsonString);
    }
    @Test
    public void test3(){
        String json = "{\"name\" : \" 王五 \",\"age\" :20}";
        Person person = JSON.parseObject(json, Person.class);
        System.out.println(person);

    }
}
